package com.zjh.photoshopstudy.app.collection;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.zjh.photoshopstudy.Model.Pager;
import com.zjh.photoshopstudy.Model.VideoEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.zjh.photoshopstudy.Utils.Data.webRoot_URL;

public class CollectionHttp {

    public void deleteCollect(final CollectPresent.OnDeleteCollect onDeleteCollect, final Set<Integer> collectionIdSet, final int uid) {
        String url = webRoot_URL + "video/deleteCollect.html";
        OkGo.<String>get(url)//
                .tag(this)//
                .params("vIds", collectionIdSet.toString())
                .params("uId", uid)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            onDeleteCollect.onDeleteCollectSuccess(collectionIdSet, uid);
                        } else {
                            onDeleteCollect.onDeleteCollectFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        onDeleteCollect.onDeleteCollectFail();
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }

    public void getCollectList(final CollectPresent.OnGetCollectList onGetCollectList, final int uid) {
        String url = webRoot_URL + "video/listForCollect.html";
        final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        OkGo.<String>get(url)//
                .tag(this)//
                .params("uId", uid)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        List list = (ArrayList) map.get("data");
                        List<VideoEntity> list2 = gson.fromJson(gson.toJson(list), new TypeToken<List<VideoEntity>>() {
                        }.getType());
                        onGetCollectList.OnGetCollectListSuccess(list2, uid);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        onGetCollectList.OnGetCollectListFail();
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }

    public void getCourseList(final CollectPresent.OnGetCollectList onGetCollectList, final int uid) {
        String url = webRoot_URL + "video/listForApp.html";
        final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        Pager pager = new Pager();
        Map<String, Object> map = new HashMap<>();
        map.put("uId", uid);
        pager.setFastQueryParameters(map);
        OkGo.<String>get(url)//
                .tag(this)//
                .params("gridPager", gson.toJson(pager))
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        List list = (ArrayList) map.get("exhibitDatas");
                        List<VideoEntity> list2 = gson.fromJson(gson.toJson(list), new TypeToken<List<VideoEntity>>() {
                        }.getType());
                        onGetCollectList.OnGetCollectListSuccess(list2, uid);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        onGetCollectList.OnGetCollectListFail();
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }

    public void deleteCourse(final CollectPresent.OnDeleteCollect onDeleteCollect, final Set<Integer> courseIdSet, final int uid) {
        String url = webRoot_URL + "video/delete.html";
        String  ids = courseIdSet.toString();
        ids = ids.replace("[","");
        ids = ids.replace("]","");
        OkGo.<String>get(url)//
                .tag(this)//
                .params("ids", ids)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            onDeleteCollect.onDeleteCollectSuccess(courseIdSet, uid);
                        } else {
                            onDeleteCollect.onDeleteCollectFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        onDeleteCollect.onDeleteCollectFail();
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }
}
